package com.ybxiang.camera_opengl.utils

import android.content.Context
import android.opengl.GLES20
import android.util.Log
import java.io.BufferedReader
import java.io.InputStreamReader

/**
 * Created by yunbo.xiang on 2021/12/15
 * Describe:
 */
object OpenGLUtils {
    private const val TAG = "OpenGLUtils"

    /**
     * opengl顶点坐标系
     */
    val VERTEX = floatArrayOf(
        -1.0f, -1.0f, // 左下角坐标
        1.0f, -1.0f, // 右下角坐标
        -1.0f, 1.0f, // 左上角坐标
        1.0f, 1.0f // 右上角坐标
    )

    /**
     * 纹理坐标系
     */
    val TEXTURE = floatArrayOf(
        0.0f, 0.0f,
        1.0f, 0.0f,
        0.0f, 1.0f,
        1.0f, 1.0f
    )

    fun readRawTextFile(context: Context, rawId: Int): String {
        val inputStream = context.resources.openRawResource(rawId)
        val bufferReader = BufferedReader(InputStreamReader(inputStream))
        var line: String
        val sb = StringBuilder()
        try {
            while (bufferReader.readLine().also { line = it } != null) {
                sb.append(line)
                sb.append("\n")
            }
        } catch (e: Exception) {
            Log.e(TAG, Log.getStackTraceString(e))
        }
        return sb.toString()
    }

    fun loadProgram(vSource: String, fSource: String): Int {
        //顶点着色器
        val vShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
        //加载着色器代码
        GLES20.glShaderSource(vShader, vSource);
        //编译（配置）
        GLES20.glCompileShader(vShader);
        //查看配置 是否成功
        val status = IntArray(1)
        GLES20.glGetShaderiv(vShader, GLES20.GL_COMPILE_STATUS, status, 0);
        if (status[0] != GLES20.GL_TRUE) {//失败
            throw IllegalStateException("load vertex shader:" + GLES20.glGetShaderInfoLog(vShader));
        }
        //片元着色器 流程和上面一样
        val fShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
        //加载着色器代码
        GLES20.glShaderSource(fShader, fSource);
        //编译（配置）
        GLES20.glCompileShader(fShader);

        //查看配置 是否成功
        GLES20.glGetShaderiv(fShader, GLES20.GL_COMPILE_STATUS, status, 0);
        if (status[0] != GLES20.GL_TRUE) {//失败
            throw IllegalStateException("load fragment shader:" + GLES20.glGetShaderInfoLog(vShader));
        }

        //创建着色器程序
        val program = GLES20.glCreateProgram();
        //绑定顶点和片元
        GLES20.glAttachShader(program, vShader);
        GLES20.glAttachShader(program, fShader);
        //链接着色器程序
        GLES20.glLinkProgram(program);
        //获得状态
        GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, status, 0);
        if (status[0] != GLES20.GL_TRUE) {
            throw IllegalStateException("link program:" + GLES20.glGetProgramInfoLog(program));
        }
        GLES20.glDeleteShader(vShader);
        GLES20.glDeleteShader(fShader);

        return program
    }
}